<?php

namespace App\Console\Commands\OpenShop;

use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationCustomerAnalysisModel;
use Illuminate\Console\Command;

class CustomerAnalysisCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'CustomerAnalysisCommand';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '客群分析脏数据修复';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        OpenShopSiteLocationCustomerAnalysisModel::query()
                                                 ->get()
                                                 ->each(function (
                                                     OpenShopSiteLocationCustomerAnalysisModel $analysisModel
                                                 ) {
                                                     $weekIndex = date("w", $analysisModel->time);
                                                     $week      = ($weekIndex == 0 ? 7 : $weekIndex);
                                                     if ($analysisModel->week != $week) {
                                                         $this->output->info([
                                                             "id"           => "主键id - " . $analysisModel->id,
                                                             "time"         => "当前时间 - " . formatDateYmd($analysisModel->time),
                                                             "actuallyWeek" => "实际周 - " . $week,
                                                             "week"         => "错误周 - " . $analysisModel->week,
                                                         ]);

                                                         $analysisModel->setRawAttributes(["week" => $week])->save();
                                                     }
                                                 });
        return 0;
    }
}
